var arr = [];
var count = 5;
var page = 1;
var r;
// 权限管理
var user = JSON.parse(localStorage.getItem('user'));
console.log(user.type);
console.log(user.name);
console.log(user.id)
// 判断进入
if (localStorageu.ser.length) {


    // 获取数据
    function rwt() {
        $.ajax({
            url: '/api/dorm/getdorm',
            success: function (res) {
                console.log(res);
                if (res.data.length) {
                    arr = res.data;
                    rem();
                    ret();
                }
            }
        })
    }
    rwt();

    function rem() {
        $('.motaikuang table tbody').html('');
        $.each(arr.slice((page - 1) * count, page * count), function (i, v) {
            var n = v.type == '1' ? '正常' : '催款中';
            $('.motaikuang table tbody').append(' <tr data-id="' + v.id + '">\
                    <td class="th1">'+ v.dormId + '</td>\
                    <td>'+ v.dormName + '</td>\
                    <td>'+ v.num + '</td>\
                    <td>'+ v.balance + '</td>\
                    <td>'+ n + '</td>\
                    <td>\
                            <button type="button" class="shu" data-toggle="modal" data-target="#myModal5">\
                                删除\
                            </button>\
                            <button type="button" class="shu" data-toggle="modal" data-target="#exampleModal5"\
                                data-whatever="@mdo">修改</button>\
                            <button type="button" class="shui" data-toggle="modal" data-target="#Modal5">\
                                发起催款申请\
                    </td>\
                </tr>')
        })
    }
    $('#nav2').on('click', 'li', function () {
        page = $(this).text();
        rem();
    })
    // 创建页码

    function ret() {
        var n = Math.ceil(arr.length / count);
        $('#nav2 .diyebiao').remove();
        for (var i = 1; i <= n; i++) {
            $('.yeshu').before($('<li class="diyebiao"><a href="#">' + i + '</a></li>'));
        }

    }

    //宿舍删除
    // 当点击删除的时候
    $('#myModal5').on('show.bs.modal', function (e) {
        r = $(e.relatedTarget).parents('tr').attr('data-id');
        // console.log(n);
    })
    //当点击确定的时候
    $('.susheshanchu').on('click', function () {

        $.ajax({
            url: '/api/dorm/deldorm',
            data: {
                dormId: r
            },
            type: 'post',
            success: function (res) {
                // console.log(res)
                if (res.code == 200) {
                    $('#myModal5').modal('hide');
                    rwt();
                } else {
                    alert('删除失败')
                }
            }
        })
    })
    // 宿舍修改
    // 当点击修改的时候
    $('#exampleModal5').on('show.bs.modal', function (e) {
        var tr = $(e.relatedTarget).parents('tr');
        $('.j-s-id').val(tr.children().eq(0).text());
        $('.j-s-name').val(tr.children().eq(1).text());

        $('.j-s-wp').val(tr.children().eq(2).text());

        $('.j-s-pa').val(tr.children().eq(3).text());


    })
    // 当点击确定的时候
    $('.j-s-q').on('click', function () {
        console.log($('.j-s-wp').val())
        $.ajax({
            url: '/api/dorm/updatedorm',
            data: {
                dormId: $('.j-s-id').val(),
                dormName: $('.j-s-name').val(),
                num: $('.j-s-wp').val(),
                balance: $('.j-s-pa').val(),
                id: r
            },
            type: 'post',
            success: function (res) {

                if (res.code == 200) {
                    $('#exampleModal5').modal('hide');
                    rwt();
                } else {
                    alert("修改失败")
                }
            }
        })
    })
    // 添加宿舍
    // 当点击添加宿舍确定时
    $('.j-s-d').on('click', function () {
        $.ajax({
            url: '/api/dorm/adddorm',
            data: {
                dormId: $('.j-s-bianhao').val(),
                balance: $('.j-s-shun').val(),
                dormName: $('.j-s-suName').val(),
            },
            type: 'post',
            success: function (res) {
                if (res.code == 200) {
                    $('#exampleModal6').modal('hide');
                    rwt();
                } else {
                    alert('添加失败')
                }
            }
        })
    })
    // 发起催款申请
    var tr;
    var yyy;
    $('#Modal5').on('show.bs.modal', function (e) {
        tr = $(e.relatedTarget).parents('tr').attr('data-id');
        yyy = $(e.relatedTarget).parents('tr').children().eq(4).text();
        //    console.log(yyy)
    })


    // $(document).on('click','.shui',function(){
    //     yyy=$(this).parent().parent('tr').children().eq(4).text();
    // })
    $('.j-s-quikuanshenqing').on('click', function () {
        // console.log($('#molikuang tbody').children().children().eq(4).text()=='正常')
        var ty = yyy == '正常' ? '2' : '1';
        $.ajax({
            url: '/api/dorm/deptdorm',
            data: {
                id: tr,
                type: ty,
            },
            type: 'post',
            success: function (res) {
                if (res.code == 200) {
                    $('#Modal5').modal('hide');
                    rwt();
                } else {
                    alert('修改失败')
                }
            }
        })
    })
    //切换账号
    $('#qiehuanzhanghao').on('click', function () {
        window.location.href = '/wb258-2/page/HomePage.html'
    })
    // 退出
    $('#guanbichuangko').on('click', function () {
        closePage();
        // window.location.href="https://blog.csdn.net/kuangfengbuyi/article/details/52052301"

    })
    function closePage() {
        if (navigator.userAgent.indexOf("Firefox") != -1 || navigator.userAgent.indexOf("Chrome") != -1) {
            window.location.href = "about:blank";
            window.close();
        } else {
            window.opener = null;
            window.open("", "_self");
            window.close();
        }
    }
    //切换账号
    $('#qiehuanzhanghao').on('click', function () {
        window.location.href = '/wb258-2/page/HomePage.html'
    })
    // 退出
    $('#guanbichuangko').on('click', function () {
        closePage();
        // window.location.href="https://blog.csdn.net/kuangfengbuyi/article/details/52052301"

    })
    function closePage() {
        if (navigator.userAgent.indexOf("Firefox") != -1 || navigator.userAgent.indexOf("Chrome") != -1) {
            window.location.href = "about:blank";
            window.close();
        } else {
            window.opener = null;
            window.open("", "_self");
            window.close();
        }
    }
} else {
    window.location.href = '/wb258-2/page/HomePage.html'
}